/**
 * 绘制线事件处理方法的定义与绑定 
 */
function draw_line_bind() {
	$("canvas").unbind("mousedown");
	$("canvas").unbind("mousemove");
	$("canvas").unbind("mouseup");
	$("canvas").mousedown(function(event) {
		var offset = $("canvas").offset();
		var x = Math.floor(event.pageX - offset.left);
		var y = Math.floor(event.pageY - offset.top);
		var line = new Line();
		line.setStart_X(x);
		line.setStart_Y(y);
		line.setEnd_X(x);
		line.setEnd_Y(y);
		line.setSize(2);
		lines[lines.length] = line;
		all[all.length] = line;
		$("canvas").mousemove(function(event) {
			redraw_line(event);
		})
		
		$("canvas").mouseup(function(event) {
			redraw_line(event);
			$("canvas").unbind("mouseup");
			$("canvas").unbind("mousemove");
		})
	})
}

/**
 * 绘制线时，线终端随鼠标移动时的重绘 
 */
function redraw_line(event) {
	var line = lines[lines.length - 1];
	var offset = $("canvas").offset();
	line.setColor("#FFFFFF");
	line.setSize(4);
	line.draw();
	var x = Math.floor(event.pageX - offset.left);
	var y = Math.floor(event.pageY - offset.top);
	line.setEnd_X(x);
	line.setEnd_Y(y);
	var color = $("#current_color").css('background-color');
	line.setColor(color);
	line.setSize(2);
	redraw_all();
}
 
/**
 * 绘制圆形事件处理方法的定义与绑定 
 */
function draw_circle_bind() {
	$("canvas").unbind("mousedown");
	$("canvas").unbind("mousemove");
	$("canvas").unbind("mouseup");
	$("canvas").mousedown(function(event) {
		var offset = $("canvas").offset();
		var x = Math.floor(event.pageX - offset.left);
		var y = Math.floor(event.pageY - offset.top);
		var circle = new Circle();
		circle.setRadius_X(x);
		circle.setRadius_Y(y);
		circle.setRadius(0);
		move_x = x;
		move_y = y;
		circles[circles.length] = circle;
		all[all.length] = circle;
		
		$("canvas").mousemove(function(event) {
			redraw_circle(event);
		})
		
		$("canvas").mouseup(function(event) {
			redraw_circle(event);
			move_x = 0;
			move_y = 0;
			$("canvas").unbind("mouseup");
			$("canvas").unbind("mousemove");
		})
	})
}

/**
 * 绘制圆时，圆终端随鼠标移动时的重绘 
 */
function redraw_circle(event) {
	var offset = $("canvas").offset();
	var x = Math.floor(event.pageX - offset.left);
	var y = Math.floor(event.pageY - offset.top);
	var circle = circles[circles.length - 1];
	circle.setColor("#FFFFFF");
	circle.draw();
	var color = $("#current_color").css('background-color');
	circle.setColor(color);
	var radius_x = (x + move_x) / 2;
	var radius_y = (y + move_y) / 2;
	var width = Math.abs(move_x - x);
	var height = Math.abs(move_y - y);
	var radius = Math.sqrt(width * width + height * height) / 2;
	circle.setRadius_X(radius_x);
	circle.setRadius_Y(radius_y);
	circle.setRadius(radius);
	redraw_all();
}


/**
 * 绘制矩形事件处理方法的定义与绑定 
 */
function draw_rectangle_bind() {
	$("canvas").unbind("mousedown");
	$("canvas").unbind("mousemove");
	$("canvas").unbind("mouseup");
	$("canvas").mousedown(function(event) {
		var offset = $("canvas").offset();
		var x = Math.floor(event.pageX - offset.left);
		var y = Math.floor(event.pageY - offset.top);
		var rectangle = new Rectangle();
		rectangle.setStart_X(x);
		rectangle.setStart_Y(y);
		rectangle.setBegin_X(x);
		rectangle.setBegin_Y(y);
		rectangle.setWidth(0)
		rectangle.setHeight(0);
		rectangles[rectangles.length] = rectangle;
		all[all.length] = rectangle;
		
		$("canvas").mousemove(function(event) {
			redraw_rectangle(event);
		})	
		
		$("canvas").mouseup(function(event) {
			redraw_rectangle(event);
			$("canvas").unbind("mouseup");
			$("canvas").unbind("mousemove");
		})
	})
}

/**
 * 绘制矩形时，矩形终端随鼠标移动时的重绘 
 */
function redraw_rectangle(event) {
	var offset = $("canvas").offset();
	var end_x = Math.floor(event.pageX - offset.left);
	var end_y = Math.floor(event.pageY - offset.top);
	var rectangle = rectangles[rectangles.length - 1];
	rectangle.setColor("#FFFFFF");
	rectangle.draw();
	var start_x = rectangle.getStart_X();
	var start_y = rectangle.getStart_Y();
	var x = (start_x > end_x) ? end_x : start_x;
	var y = (start_y > end_y) ? end_y : start_y;
	var width = Math.abs(start_x - end_x);
	var height = Math.abs(start_y - end_y);
	rectangle.setBegin_X(x);
	rectangle.setBegin_Y(y);
	rectangle.setWidth(width);
	rectangle.setHeight(height);
	var color = $("#current_color").css('background-color');
	rectangle.setColor(color);
	redraw_all();
}
